import { http, domainBase } from "../../utils/http";
Page({
  data:{
    list:[],
    page:1,   //页数
    tag:true,  //   true有下一页，false没有下一页
  },

  //页面加载
  onLoad() {
    // 先请求第1页数据
    this.getlist()
  },
   /**
     *  列表页跳转到详情页  
     */
    toDetail(e){
      let id=e.currentTarget.id;
      wx.navigateTo({
        url: '/pages/detail/detail?id='+id,
      })
  },
  //页面触底
  onReachBottom(){
    if(!this.data.tag) return;    //  设置一个门
    //page累加
    this.data.page+=1;
    console.log(this.data.page,"页数");
    //再次请求
    this.getlist()
  },
  async getlist(){
    //获取页码
    let {page} = this.data;
    console.log(this.data,'this.data');
    let { data: res } = await http({
      url: "getgoodlist",
      data: { cateid: 3, page: page, size: 5, type: 1 },
    }).catch((err) => err);
    // console.log(res,'res');
    let list = res.list.goodData;

    if(list.length<5){
      // 下一页没有数据  关门
      this.setData({
        tag:false
      })
    }


    //处理图片
    list.forEach((item) => {
      item.img = domainBase + item.img;
    });
    this.setData({
      //数据拼接
      list: this.data.list.concat(list)
    });
  },
  //跳转详情页
  toDetail(e) {
    let { id } = e.currentTarget;
    wx.navigateTo({
      url: "/pages/detail/detail?id=" + id,
    });
  },
});
